/*
 * @Description: 多存一份 sessionStorage 共享给子系统，存一份token，防止tab不共享造成可以多用户登录（其实换个浏览器不也一样）
 * @Version: 0.1
 * @Autor: ghq
 * @Date: 2020-08-21 17:56:48
 * @LastEditors: GiottoLLL7
 * @LastEditTime: 2023-02-03 15:45:33
 */
import Cookies from "js-cookie"
const AppName = import.meta.env.VITE_APP_NAME
export const TokenKey = "token";
export const UserKey = AppName + "_userinfo";

export function getToken() {
  return Cookies.get(TokenKey);
}

export function setToken(token) {
  // 设置30分钟过期
  const date = new Date();
  const minutes = 115;
  date.setTime(date.getTime() + (minutes * 60 * 1000));
  sessionStorage.setItem(TokenKey, token);
  return Cookies.set(TokenKey, token, { expires: date });
}
export function removeToken() {
  sessionStorage.removeItem(TokenKey);
  return Cookies.remove(TokenKey);
}

export function getUser() {
  const userInfo = sessionStorage.getItem(UserKey)
  if (userInfo) {
    return JSON.parse(userInfo)
  }
  return Cookies.get(UserKey) ? JSON.parse(Cookies.get(UserKey)) : null;
}
export function setUser(user) {
  sessionStorage.setItem(UserKey, JSON.stringify(user));
  return Cookies.set(UserKey, JSON.stringify(user));
}
export function removeUser() {
  sessionStorage.removeItem(UserKey);
  return Cookies.remove(UserKey);
}

export function setUserOperateTime(time) {
  sessionStorage.setItem("operateTime",time);
}

export function getUserOperateTime() {
  return sessionStorage.getItem("operateTime")

}
export function  removeUserOperateTime() {
  sessionStorage.removeItem("operateTime");

}
